home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / mandel.arc / MANDMAKE.C < prev    next >
Text File  |  1987-03-11  |  11KB  |  279 lines

  1. /****************************************************************************/
  2. /* MODULE NAME:                                 */
  3. /*    MANDMAKE - Nmemonic "compute MANDelbrot set and MAKE an image file"   */
  4. /*                                        */
  5. /* MODULE DESCRIPTION:                                */
  6. /*    Computes, and stores the itteration number for each pixle in a given  */
  7. /*    area with a specified magnification.                    */
  8. /*                                        */
  9. /* USAGE:                                    */
  10. /*    mandel begin_c_real begin_c_imag Xdots Ydots Range Output_file_name   */
  11. /*                                        */
  12. /* INPUT:                                    */
  13. /*    double  A_begin    The real portion of the beginning complex coordinate*/
  14. /*    double  B_begin    The imaginary portion of the beginning coordinate   */
  15. /*    int     Xdots    The number of pixles of the desplay device in X.    */
  16. /*    int     Ydots    The number of pixles of the desplay device in Y.    */
  17. /*    int     Range    The zoom factor to use for the display.         */
  18. /*    FILE    Out_fl_nm The file that will contain the itteration data.     */
  19. /*                                        */
  20. /* PROCESS:  (Pseudo code)                            */
  21. /*     Initialize global constants.                        */
  22. /*     Define parameters.                            */
  23. /*     Begin MANDEL.                                */
  24. /*        Define local variables.                        */
  25. /*        --- Insert pseudo code here ---                    */
  26. /*     End MANDEL.                                */
  27. /*                                        */
  28. /* OUTPUT:                                    */
  29. /*                                        */
  30. /* EQUATIONS/ALGORITHMS:                            */
  31. /*                                        */
  32. /* REFERENCES:                                    */
  33. /*                                        */
  34. /****************************************************************************/
  35.  
  36. /*---> Standard definitions and declares <---*/
  37. #include <stdio.h>
  38. #include <dos.h>
  39.  
  40. /*---> Initialize global constants <---*/
  41. #define then
  42. #define begin {
  43. #define end }
  44. #define TRUE 1
  45. #define FALSE 0
  46. extern    double atof() ;
  47. extern    double sqrt() ;
  48.  
  49. /*---> Define non-standard stack size <---*/
  50. int    _stack    =  20000 ;
  51.  
  52. main( ARGC, ARGV )
  53.  
  54.    /*---> Define Parameters <---*/
  55.    int             ARGC         ; /*                   */
  56.    char         *ARGV[]         ; /*                   */
  57.  
  58. begin    /*MANDEL*/
  59.  
  60.    /*---> Define "variable" Local Variables <---*/
  61.    int             ITTER     = 1000; /* Maximum number of itterations/pel*/
  62.    double         HIST[1024]      ; /* Histogram               */
  63.  
  64.    /*---> Define Local Variables <---*/
  65.    FILE         *tp        =  0  ;
  66.    FILE         *fp        =  0  ;
  67.    int             INDX      =  0  ; /* General purpose counter.       */
  68.    int             INDY      =  0  ; /* General purpose counter.       */
  69.    int             INDZ      =  0  ; /* General purpose counter.       */
  70.    int             XDOTS     =  0  ; /* Number of display pixles in X    */
  71.    int             YDOTS     =  0  ; /* Number of display pixles in Y    */
  72.    int             RET_CODE  =  0  ;
  73.    int             COLOR     =  0  ;
  74.    int             Z           =  0  ; /*                   */
  75.    double         XINC      = 0.0 ; /* Distance between pixles       */
  76.    double         YINC      = 0.0 ; /* Distance between pixles       */
  77.    double         A_BEGIN   = 0.0 ; /* Beginning coordinate. Real.       */
  78.    double         B_BEGIN   = 0.0 ; /* Beginning coordinate. Imaginary. */
  79.    double         RANGE     = 0.0 ; /* Zoom factor               */
  80.    double         CA        = 0.0 ; /* Current coordinate. Real.       */
  81.    double         CB        = 0.0 ; /* Current coordinate. Imaginary.   */
  82.    double         ZA        = 0.0 ; /* Current itteration. Real.       */
  83.    double         ZB        = 0.0 ; /* Current itteration. Imaginary.   */
  84.    double         ZSIZE     = 0.0 ; /* Real distance of Z(A,B) from 0,0 */
  85.    double         ZNEWA     = 0.0 ; /* Next itteration. Real.       */
  86.    double         ZNEWB     = 0.0 ; /* Next itteration. Immaginary.       */
  87.    double         N           = 0.0 ; /* Running sum of PELS computed       */
  88.    double         ZSUM      = 0.0 ; /* Running sum of INDZ (itterations)*/
  89.    double         ZSUMSQ    = 0.0 ; /* Running sum of squares of INDZ   */
  90.    double         BAND[12]         ; /*                   */
  91.    double         COUNT         ; /*                   */
  92.    char          IN_BUF[80]      ; /*                   */
  93.  
  94.    /*---> Process runtime arguments <---*/
  95.    if( ARGC < 7 ) then begin
  96.       printf( "INVALID ARGUMENT COUNT of %d . . . . . .\n", ARGC ) ;
  97.       printf( "Use :\n" ) ;
  98.       printf( "mandmake X_real Y_imag  Xdots  Ydots  Range    Out_file\n\n");
  99.  
  100.       printf( "  e.g.  -2.000  -1.250   320   200    2.500   mandata.tmp\n" ) ;
  101.       printf( "        -1.500  -0.500   320   200    0.500   mandata.tmp\n" ) ;
  102.       printf( "        -1.250  -0.200   320   200    0.100   mandata.tmp\n" ) ;
  103.       printf( "        -1.250  -0.180   320   200    0.030   mandata.tmp\n" ) ;
  104.       printf( "        -1.235  -0.171   320   200    0.006   mandata.tmp\n" ) ;
  105.       printf( "        -1.233  -0.169   320   200    0.001   mandata.tmp\n" ) ;
  106.       printf( "        -1.233  -0.169   320   200    0.0001  mandata.tmp\n" ) ;
  107.       printf( "        -1.233  -0.169   320   200    0.00001 mandata.tmp\n" ) ;
  108.       return ;
  109.       end   /*if*/
  110.    A_BEGIN =  atof( ARGV[ 2 ] ) ;
  111.    B_BEGIN =  atof( ARGV[ 1 ] ) ;
  112.    XDOTS   =  atoi( ARGV[ 3 ] ) ;
  113.    YDOTS   =  atoi( ARGV[ 4 ] ) ;
  114.    RANGE   =  atof( ARGV[ 5 ] ) ;
  115.  
  116.    /*---> Initialize <---*/
  117.    for( INDX=0; INDX < 1005; INDX++ ) HIST[ INDX ] = 0.0 ;
  118.    for( INDX=0; INDX <     12; INDX++ ) BAND[ INDX ] = 0.0 ;
  119.  
  120.    /*---> Open output file <---*/
  121.    tp = fopen( "mandtemp.dat", "w" ) ;
  122.    if( tp == 0 ) then begin
  123.       printf("Open ERROR on output file  : %s \n", "mandtemp.dat" ) ;
  124.       return ;
  125.       end   /*if*/
  126.  
  127.    /*---> Compute complex spacing between pixles <---*/
  128.    XINC = RANGE / (float)XDOTS ;
  129.    YINC = RANGE / (float)YDOTS ;
  130.  
  131.    /*---> Loop to compute each pixle column value on 2-D Complex grid <---*/
  132.    for( INDY=0; INDY < YDOTS; INDY++ ) begin
  133.  
  134.       /*---> Initialize for new row <---*/
  135.       CA = YINC * INDY + B_BEGIN ;
  136.  
  137.       for( INDX=0; INDX < XDOTS; INDX++ ) begin
  138.  
  139.      /*---> Initialize for new column ( and pixle ) <---*/
  140.      ZA = 0.0 ;
  141.      ZB = 0.0 ;
  142.      CB = XINC * INDX + A_BEGIN ;
  143.      ZSIZE = sqrt(    CA*CA + CB*CB ) ;
  144.  
  145.      /*---> Loop to itterate for each ( Z**2 + C ) <---*/
  146.      for( INDZ=0; (ZSIZE < 2) && (INDZ <= ITTER); INDZ++ ) begin
  147.         ZNEWA = ( ZA*ZA ) - ( ZB*ZB ) + CA ;
  148.         ZNEWB = ( 2*ZA*ZB ) + CB ;
  149.         ZSIZE = sqrt( ZNEWA*ZNEWA + ZNEWB*ZNEWB ) ;
  150.         ZA      = ZNEWA ;
  151.         ZB      = ZNEWB ;
  152.         end   /* for INDZ */
  153.  
  154.      /*---> Compute statistics <---*/
  155.      HIST[ INDZ ]++ ;
  156.  
  157.      /*---> Output results <---*/
  158.      fprintf( tp, "%d\n", INDZ ) ;
  159.  
  160.      end   /*for INDX*/
  161.  
  162.       /*---> Check for user requested exit. <---*/
  163.       RET_CODE = kbhit() ;
  164.       if( RET_CODE ) then begin
  165.      N    = (float)INDY * (float)INDX  ;
  166.      INDX = XDOTS ;
  167.      INDY = YDOTS ;
  168.      end   /*if*/
  169.  
  170.       /*---> Report progress to user. <---*/
  171.       printf("Processing for line %d is complete\n", INDY ) ;
  172.  
  173.       end   /*for INDY*/
  174.  
  175.    /*---> Display input and computational parameters <---*/
  176.    printf("If you wish the statistical summary to be printed rather than\n" ) ;
  177.    printf("just displayed on the CRT, then : \n" ) ;
  178.    printf("            Turn on the printer,\n" ) ;
  179.    printf("            Then push <ctrl> <PrtSc>\n" ) ;
  180.    printf("            Finally, push <return> to continue :\n" ) ;
  181.    printf("Otherwise just push <return> to continue :\n" ) ;
  182.    gets() ;
  183.  
  184.    printf( "\n\n\n************ Mnadelbrot Set Computation system ************\n" ) ;
  185.    printf( "\nInput parameters were as follows :\n\n" ) ;
  186.    printf( "     Beginning Coordinate (real) :  %f\n", A_BEGIN  ) ;
  187.    printf( "     Beginning Coordinate (imag) :  %f\n", B_BEGIN  ) ;
  188.    printf( "     Size (X) of screen          :  %d\n", XDOTS    ) ;
  189.    printf( "     Size (Y) of screen          :  %d\n", YDOTS    ) ;
  190.    printf( "     Range                       :  %e\n", RANGE    ) ;
  191.    printf( "     Increment between pels in X :  %e\n", XINC     ) ;
  192.    printf( "     Increment between pels in Y :  %e\n", YINC     ) ;
  193.    printf( "     Itterations per pel         :  %d\n", ITTER    ) ;
  194.    printf( "     Output file name            :  %s\n", ARGV[6] ) ;
  195.  
  196.    if( N == 0.0 ) then    N = (float)XDOTS * (float)YDOTS ;
  197.  
  198.    printf("Total Count was actually %f pels.\n", N ) ;
  199.    printf( "Total expected pels was %f \n", (float)XDOTS * (float)YDOTS ) ;
  200.  
  201.    printf( "\nHISTOGRAM summary : \n" ) ;
  202.    for( INDX=0; INDX < ITTER; INDX += 10 ) begin
  203.       if( (INDX % 100) == 0 ) then printf("\n" ) ;
  204.       printf( "%4d-%4d   %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f \n",
  205.      INDX, INDX+9,
  206.      HIST[INDX  ],HIST[INDX+1],HIST[INDX+2],HIST[INDX+3],HIST[INDX+4],
  207.      HIST[INDX+5],HIST[INDX+6],HIST[INDX+7],HIST[INDX+8],HIST[INDX+9] );
  208.       end   /*for*/
  209.    printf("\n1000-       %5.0f\n", HIST[ 1000 ] ) ;
  210.  
  211.    /*---> (Re)open file(s) <---*/
  212.    fclose( tp ) ;
  213.    tp = fopen( "mandtemp.dat", "r" ) ;
  214.    if( tp == 0 ) then begin
  215.       printf("Open ERROR on input file  : %s \n", "mandtemp.dat" ) ;
  216.       return ;
  217.       end   /*if*/
  218.    fp = fopen( ARGV[6], "w" ) ;
  219.    if( fp == 0 ) then begin
  220.       printf("Open ERROR on output file  : %s \n", ARGV[6] ) ;
  221.       return ;
  222.       end   /*if*/
  223.  
  224.    /*---> Output size <---*/
  225.    fprintf( fp, "%d\n", XDOTS ) ;
  226.    fprintf( fp, "%d\n", YDOTS ) ;
  227.  
  228.    /*---> Compute values for color bands <---*/
  229.    COUNT = 0.0 ;
  230.    N =    N / 10.0 ;
  231.    INDZ = 0 ;
  232.    for( INDX=0; INDX <= 1000; INDX++ ) begin
  233.       COUNT += HIST[ INDX ] ;
  234.       if( COUNT < N )          then BAND[ 0 ] = (float)INDX ;
  235.      else if( COUNT < 2.0*N ) then BAND[ 1 ] = (float)INDX ;
  236.      else if( COUNT < 3.0*N ) then BAND[ 2 ] = (float)INDX ;
  237.      else if( COUNT < 4.0*N ) then BAND[ 3 ] = (float)INDX ;
  238.      else if( COUNT < 5.0*N ) then BAND[ 4 ] = (float)INDX ;
  239.      else if( COUNT < 6.0*N ) then BAND[ 5 ] = (float)INDX ;
  240.      else if( COUNT < 7.0*N ) then BAND[ 6 ] = (float)INDX ;
  241.      else if( COUNT < 8.0*N ) then BAND[ 7 ] = (float)INDX ;
  242.      else if( COUNT < 9.0*N ) then BAND[ 8 ] = (float)INDX ;
  243.       end   /*for*/
  244.  
  245.    printf("Band boundries are : \n" ) ;
  246.    for(INDX=0;INDX<=8;INDX++) printf("   %4.0f", BAND[ INDX ] ) ;
  247.    printf("\n");
  248.  
  249.    /*---> Loop for each pixle <---*/
  250.    for( INDY=0; INDY < YDOTS; INDY++ ) begin
  251.       for( INDX=0; INDX < XDOTS; INDX++ ) begin
  252.  
  253.      /*---> Read value of pixle from input file <---*/
  254.      fgets( IN_BUF, 80, tp ) ;
  255.      Z = atoi( IN_BUF ) ;
  256.  
  257.      /*---> Compute pixle color value <---*/
  258.      COLOR = 16 ;
  259.      if(         (float)Z < BAND[0] ) then COLOR = 1 ;
  260.         else if( (float)Z < BAND[1] ) then COLOR = 2 ;
  261.         else if( (float)Z < BAND[2] ) then COLOR = 3 ;
  262.         else if( (float)Z < BAND[3] ) then COLOR = 1 ;
  263.         else if( (float)Z < BAND[4] ) then COLOR = 2 ;
  264.         else if( (float)Z < BAND[5] ) then COLOR = 3 ;
  265.         else if( (float)Z < BAND[6] ) then COLOR = 1 ;
  266.         else if( (float)Z < BAND[7] ) then COLOR = 2 ;
  267.         else if( (float)Z < BAND[8] ) then COLOR = 3 ;
  268.                    else COLOR = 0 ;
  269.  
  270.      /*---> Output results <---*/
  271.      fprintf( fp, "%d\n", COLOR ) ;
  272.  
  273.      end   /*for X*/
  274.       end   /*for Y*/
  275.  
  276.    /*---> Exit <---*/
  277.    return ;
  278.    end     /* MANDEL */
  279.